package com.ipet.app.adapter.ViewHolder;

import android.content.Intent;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.CardView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ipet.app.R;
import com.ipet.app.bean.HotPost;
import com.ipet.app.ui.activity.PostDetailActivity;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

/**
 * Created by jindongping on 16/3/18.
 */
public class PostHotViewHolderNormal extends BaseViewHolder<HotPost> {
    LinearLayout lineAgree, lineReply;
    CardView cardView;
    TextView name, time, title, content;
    TextView agreeIc, replyIc, agreeTv;

    public PostHotViewHolderNormal(ViewGroup parent, @LayoutRes int res) {
        super(parent, res);
        lineAgree = $(R.id.line_agree);
        lineReply = $(R.id.line_reply);
        cardView = $(R.id.card_view);
        name = $(R.id.post_name);
        time = $(R.id.post_time);
        title = $(R.id.post_title);
        content = $(R.id.post_content);
        agreeIc = $(R.id.ic_agree);
        replyIc = $(R.id.ic_reply);
        agreeTv = $(R.id.tv_agree);
    }

    @Override
    public void setData(final HotPost data) {
        super.setData(data);
        //在此处设置监听 绑定数据
        name.setText(data.getNickname());
        time.setText(data.getCreate_time());
        if (data.getTheme() != null && data.getTheme().length() > 1) {
            title.setVisibility(View.VISIBLE);
            title.setText(data.getTheme());
        }
        content.setText(data.getContent());

        //设置点赞
        PostHotViewHolderImg.AgreeListener agreeListener = new PostHotViewHolderImg.AgreeListener(data,agreeTv,agreeIc,lineAgree);
        PostHotViewHolderImg.CancelAgreeListener cancelAgreeListener = new PostHotViewHolderImg.CancelAgreeListener(data,agreeTv,agreeIc,lineAgree);
        agreeListener.setCancelAgreeListener(cancelAgreeListener);
        cancelAgreeListener.setAgreeListener(agreeListener);
        if (data.getIsAgree() == null) {
            //没有点赞
            agreeIc.setBackgroundResource(R.mipmap.ic_disagree);
            agreeTv.setText("" + data.getAgree_num());
            lineAgree.setOnClickListener(agreeListener);
        } else {
            //已经点赞了
            agreeIc.setBackgroundResource(R.mipmap.ic_agree);
            lineAgree.setOnClickListener(cancelAgreeListener);
            agreeTv.setText("" + data.getAgree_num());
        }

        //设置评论按钮跳转
        lineReply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PostDetailActivity.class);
                intent.putExtra("hotPost", data);
                getContext().startActivity(intent);
            }
        });
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PostDetailActivity.class);
                intent.putExtra("hotPost", data);
                getContext().startActivity(intent);
            }
        });

    }
}